浏览量 4578
2019/01/10 15:05
1.fetch方法:
App.vue
export default {
name: 'App',
data() {
return {}
},
//fetch
created() {
fetch("/apis/test/testToken.php", {
method:"post",
headers:{
token:"f4c9023jkwalkfjas2910a"
},
body:JSON.stringify(
{username:"brownwang",password:"21232"}
)
.then(result =>{
return result.json()
})
.then(data => {
console.log(data)
})
})
}
}
/config/index.js
proxyTable: {
'/apis':{
target:'https://goods.footer.com', //接口名称
changeOrigin:true, //是否跨域
pathRewrite:{
'^/apis':'' //需要rewrite重写的
}
}
},
2.axios
npm install axios
配置main.js
import axios from 'axios'
axios.defaults.headers.common['token']='12fdafsafdfsadfsaf'
axios.defaults.headers.post["Content-type"]="application/json"
Vue.prototype.$axios=axios
App.vue实现:
export default {
name: 'App',
data() {
return {}
},
//axios
created() {
this.$axios.post("/apis/test/testToken.php",
{username:"brownwang",password:"1232"})
.then(data=>{
console.log(data)
})
}
}